package com.htg.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class DialogStandardTwoButton {

    private Context cxt;

    private AlertDialog alertDialog = null;
    private int colorPrimary;
    private float cornerRadius;


    public DialogStandardTwoButton(Context cxt) {
        this.cxt = cxt;
        this.colorPrimary = cxt.getResources().getColor(R.color.blue_0166FF, cxt.getTheme());
        this.cornerRadius = cxt.getResources().getDimensionPixelSize(R.dimen.dp_5);
    }

    public void show(String content, String leftButtonText, String rightButtonText, OnClickListener leftButtonOnClickLis, OnClickListener rightButtonOnClickLis) {
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
            alertDialog = builder.create();
            alertDialog.setCancelable(false);
        } else {
            if (alertDialog.isShowing()) {
                return;
            }
        }
        alertDialog.show();
        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_standard_twobutton, null);
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(R.drawable.shape_white_rounded_12dp);
        WindowManager.LayoutParams params = window.getAttributes();
        int width = cxt.getResources().getDimensionPixelSize(R.dimen.dp_280);

        params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        // 创建一个 GradientDrawable 对象
        GradientDrawable drawableRoot = new GradientDrawable();
        drawableRoot.setShape(GradientDrawable.RECTANGLE);
        drawableRoot.setColor(cxt.getResources().getColor(R.color.white, cxt.getTheme())); // 设置背景颜色
        drawableRoot.setCornerRadius(cornerRadius);// 设置圆角半径
        window.setBackgroundDrawable(drawableRoot);
        window.setContentView(view);

        TextView tvContent = view.findViewById(R.id.tv_dialog_standard_twobutton_content);
        TextView tvLeftButton = view.findViewById(R.id.tv_dialog_standard_twobutton_leftbutton);
        TextView tvRightButton = view.findViewById(R.id.tv_dialog_standard_twobutton_rightbutton);


        tvContent.setText(content);
        tvLeftButton.setText(leftButtonText);
        tvRightButton.setText(rightButtonText);
        tvLeftButton.setTextColor(Color.BLACK);
        tvRightButton.setTextColor(colorPrimary);

        tvLeftButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftButtonOnClickLis != null)
                    leftButtonOnClickLis.onClick(alertDialog);
            }
        });
        tvRightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rightButtonOnClickLis != null)
                    rightButtonOnClickLis.onClick(alertDialog);
            }
        });


    }


    public void dismis() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        alertDialog = null;
    }

    public void setColorPrimary(int colorPrimary) {
        this.colorPrimary = colorPrimary;
    }

    public void setCornerRadius(float cornerRadius) {
        this.cornerRadius = cornerRadius;
    }


    public interface OnClickListener {
        void onClick(AlertDialog alertDialog);
    }


}
